import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  HttpException,
  Query,
  Req,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { InformationService } from './information.service';
import { PublicInformationDto } from './dto/information.dto';
import { TokenAuth } from '../../common/gaurd/tokenAuth.gaurd';
import { ApiHeader, ApiHeaders, ApiTags } from '@nestjs/swagger';
import { CacheKey } from '@nestjs/cache-manager';

@Controller('information')
@ApiTags('Information')
@UseGuards(TokenAuth)
export class InformationController {
  constructor(private readonly informationService: InformationService) {}
  @Post('/public')
  @HttpCode(200)
  async publicInformation(@Body() info: PublicInformationDto, @Req() request: any) {
    let user_id = (request.user_info as any).id;
    let result = await this.informationService.publicInformation(
      user_id,
      info.content,
      info.title,
      info.dayjs,
      info.type,
    );
    if (result.id > 0) {
      return {
        code: 1,
        message: '文章发布成功',
      };
    }else{
      throw new HttpException('发布失败',200)
    }
  }
  @Post('/addType')
  /**
   * @author nie
   * @description 添加分类
   * @email a896211755@gmail.com
   */
  async addType(@Query('name') name: string) {
    console.log(name);
    if (!name) throw new HttpException({ code: 0, message: 'name不能为空' }, 200);
    let val = await this.informationService.addType(name);
    if (val.id > 0) {
      return {
        code: 1,
        message: '添加成功',
      };
    } else {
      throw new HttpException({ code: 0, message: '添加失败' }, 200);
    }
  }

  @CacheKey('custom_key')
  @Get('/getTypeList')
  async getTypes() {
    let result = await this.informationService.getTypeList();
    return {
      code: 1,
      data: result,
    };
  }
}
